package example.pattern.cor;

public abstract class Support {
	private String name;
	
	private Support next;
	
	
	public Support(String name) {
		super();
		this.name = name;
	}

	public Support setNext(Support next){
		this.next = next;
		return next;
	}
	
	public final boolean support(Trouble trouble) {
		boolean resolved = resolve(trouble);
		if (resolved) {
			done(trouble);
		} else if (this.next != null) {
			resolved = this.next.support(trouble);
		} else {
			fail(trouble);
		}
		return resolved;
	}
	
	protected abstract boolean resolve(Trouble trouble);
	
	
	protected final void fail(Trouble trouble){
		System.out.println(trouble  + " has not solved trouble yet");
	}
	
	protected final void done(Trouble trouble){
		System.out.println(name  + " has solved trouble " + trouble);
	}

	@Override
	public String toString() {
		return "Support [name=" + name + ", next=" + next + "]";
	}
	
	
	
}
